package com.ptu.spzx.manager.utils;

import com.ptu.spzx.model.entity.system.SysDept;

import java.util.ArrayList;
import java.util.List;

public class DeptHelper {
    //递归实现封装过程
    public static List<SysDept> buildTree(List<SysDept> sysDeptList){
        List<SysDept> trees=new ArrayList<>();
        //遍历所有菜单
        for(SysDept sysDept:sysDeptList){
            //找到递归操作的入口 第一层菜单
            //条件：parent_id=0;
            if(sysDept.getParentId().longValue()==0){
                System.out.println(sysDept.getParentId());
                //根据第一层找到下层的数据
                trees.add(findChildren(sysDept,sysDeptList));
            }
        }
        return trees;
    }
    //递归查询下层菜单
    public static SysDept findChildren(SysDept sysDept, List<SysDept> sysDeptList) {
        sysDept.setChildren(new ArrayList<>());
        for(SysDept it:sysDeptList){
            if(sysDept.getId().longValue()==it.getParentId().longValue()){
                sysDept.getChildren().add(findChildren(it,sysDeptList));
            }
        }
        return sysDept;
    }
}
